package com.atguigu0.core

import java.sql.{DriverManager, ResultSet}

import org.apache.spark.rdd.JdbcRDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 * @description: xxx
 * @time: 2020/6/14 18:04
 * @author: baojinlong
 **/
object TestMysqlRDD {
  def main(args: Array[String]): Unit = {
    val sparkConf: SparkConf = new SparkConf().setAppName("myWordCount").setMaster("local[*]")
    // 创建sparkContext对象
    val sc: SparkContext = new SparkContext(sparkConf)
    // 设置mysql参数
    val driver = "com.mysql.jdbc.Driver"
    val url = "jdbc:mysql://rm-2zear6g88pp7z8s5zco.mysql.rds.aliyuncs.com:3306/t12818_cdp_analysis"
    val userName = "oam"
    val password = "1qaz@WSX"
    val jdbcRddValue = new JdbcRDD[Int](sc, () => {
      Class.forName(driver)
      DriverManager.getConnection(url, userName, password)
    }, "SELECT id FROM `analysis_modules` where id >? and id <? limit 10", 3009, 3374, 1, _.getInt(1))
    // 打印结果
    jdbcRddValue.foreach(println)
  }
}
